import { defineStore } from 'pinia';
import store from '@/store';

import { getCurrentRoleList } from '@/api/system/role';
import type { RoleListRow } from '@/api/system/role';

interface RoleState {
	roleList: RoleListRow[];
}

export const useRoleStore = defineStore('app-role', {
	state: (): RoleState => ({
		roleList: [],
	}),
	actions: {
		getCurrentRoleList() {
			return new Promise(async (resolve, reject) => {
				try {
					const { results } = await getCurrentRoleList();
					this.roleList = results;
					resolve(results);
				} catch (error) {
					reject(error);
				}
			});
		},
	},
});

// Need to be used outside the setup
export function useRoleStoreWithOut() {
	return useRoleStore(store);
}
